//
//  WelcomeViewController.m
//  AiShangJinFuSalesman
//
//  Created by AiShangTong on 2017/7/5.
//  Copyright © 2018年 AiShangTong. All rights reserved.
//

#import "WelcomeViewController.h"
#import "WelcomeView.h"

@interface WelcomeViewController ()

@property(nonatomic,strong)UIPageControl *control;

@end

@implementation WelcomeViewController

static NSString *ID = @"cell";

- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView registerClass:[WelcomeView class] forCellWithReuseIdentifier:ID];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self setUpPageControl];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self updateNewData];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}


- (void)setUpPageControl
{
    UIPageControl *control = [[UIPageControl alloc] init];
    control.numberOfPages = 3;
    control.pageIndicatorTintColor = [UIColor blackColor];
    control.currentPageIndicatorTintColor = [UIColor redColor];
    control.frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 40, CGRectGetWidth(self.view.frame), 20);
    _control = control;
    [_control addTarget:self action:@selector(ChagePage:) forControlEvents:UIControlEventValueChanged];
    // [self.view addSubview:control];
}
-(void)ChagePage:(UIPageControl*)PageControl{
    NSInteger index = PageControl.currentPage;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]    atScrollPosition:0 animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
    _control.currentPage = page;
}

- (void)updateNewData {
    
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    NSString *jiaoYanAPP = [userD objectForKey:@"ChuShiHuaWeiZhengChangAPP_Mao"];
    if ([jiaoYanAPP isEqualToString:@"999"]) {
        [userD setObject:@"000" forKey:@"ChuShiHuaWeiZhengChangAPP_Mao"];
        [userD setObject:@"0" forKey:@"IsShowLoginInfo_Mao"];
    }
}

#pragma mark - UICollectionView代理和数据源

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WelcomeView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    NSString *imageName;
    switch (indexPath.row) {
        case 0:
        {
            if (ScreenHeight > 800.0) {
                imageName = @"iphoneX1";
            }else{
                imageName = @"welcomeToHFenQiOne";
            }
            
        }
            break;
        case 1:
        {
            if (ScreenHeight > 800.0) {
                imageName = @"iphoneX2";
            }else{
                imageName = @"welcomeToHFenQiTwo";
            }
            
        }
            break;
        case 2:
        {
            if (ScreenHeight > 800.0) {
                imageName = @"iphoneX3";
            }else{
                imageName = @"welcomeToHFenQiThree";
            }
            
        }
            break;
        default:
            break;
    }
    

    cell.image = [UIImage imageNamed:imageName];
    
    return cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 2) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
